import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { getUserInfoReq } from "@/api/user";

export const useUserStore = defineStore(
  "counter",
  () => {
    const username = ref("");
    const token = ref(null);
    const role = ref("");
    const checkedKeys = ref("");
    //服务器返回的 按钮权限信息
    // const auths = ref(["admin:list:delete", "admin:list:add"]);
    const auths = ref([]);

    setTimeout(() => {
      auths.value = ["admin:list:delete"];
    }, 1000);
    //admin:list:delete
    //角色：页面：按钮

    //获取用户信息
    // function getUserInfo(callback) {
    //   getUserInfoReq(username.value).then((r) => {
    //     console.log(r.data);
    //     //保存用户的角色信息和 权限信息
    //     //role  2  -》 超级管理员
    //     role.value = r.data.data[0].role;
    //     checkedKeys.value = r.data.data[0].checkedKeys;
    //     // console.log();
    //     callback();
    //   });
    // }

    function getUserInfo() {
      let p = new Promise((resolve, reject) => {
        getUserInfoReq(username.value).then((r) => {
          console.log(r.data);
          //保存用户的角色信息和 权限信息
          //role  2  -》 超级管理员
          role.value = r.data.data[0].role;
          checkedKeys.value = ["首页", ...r.data.data[0].checkedKeys];
          // console.log();
          resolve();
        });
      });

      return p;
    }

    return { username, token, getUserInfo, role, checkedKeys, auths };
  },
  {
    persist: true
  }
);
